package com.example.springboot.service.impl;

import com.example.springboot.controller.request.BorrowPageRequest;

import com.example.springboot.entity.Borrow;
import com.example.springboot.mapper.BookMapper;
import com.example.springboot.mapper.BorrowMapper;
import com.example.springboot.service.IBorrowService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class BorrowService implements IBorrowService {
    @Resource
    BorrowMapper borrowMapper;
    @Resource
    BookMapper bookMapper;

    public double getTodayBorrowRate() { //计算当日借阅率
        int todayBorrows = borrowMapper.countTodayBorrows();
        int totalBooks = bookMapper.countTotalBooks();
        if (totalBooks == 0) {
            return 0;
        }
        return (double) todayBorrows / totalBooks;
    }

    @Override
    public int count(String search) {
        return borrowMapper.count(search);
    }

    @Override
    public void updateStatus(int recordId, int status) {
        borrowMapper.updateStatus(recordId, status);
    }

    @Override
    public List<Borrow> list() {
        return null;
    }

    @Override
    public Object page(BorrowPageRequest borrowPageRequest) {
        PageHelper.startPage(borrowPageRequest.getPageNum(),borrowPageRequest.getPageSize());
        List<Borrow> borrow = borrowMapper.listByCondition(borrowPageRequest);
        return new PageInfo<>(borrow);
    }

    public void addBorrow(Borrow borrow) {
        borrow.setBorrowDate(new Date()); // 设置当前时间戳为借阅时间
        borrowMapper.addBorrow(borrow);
    }
}